package com.pmt.feedplayer.ui

import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pmt.feedplayer.data.LocalVideoScanner
import com.pmt.feedplayer.data.VideoItem
import com.pmt.feedplayer.data.VideoListState
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 视频列表ViewModel
 * 负责加载和管理本地视频列表数据
 */
class VideoListViewModel(private val videoScanner: LocalVideoScanner) : ViewModel() {
    
    // 视频列表状态
    val videoListState = mutableStateOf<VideoListState>(VideoListState.Loading)
    
    // 选中的视频
    val selectedVideo = mutableStateOf<VideoItem?>(null)
    
    // 是否显示播放器
    val showPlayer = mutableStateOf(false)
    
    // 当前视频索引
    val currentVideoIndex = mutableStateOf(-1)
    
    // 是否隐藏底部导航栏
    val hideBottomNavBar = mutableStateOf(false)
    
    /**
     * 加载本地视频
     */
    fun loadLocalVideos(limit: Int) {
        viewModelScope.launch {
            try {
                // 模拟加载延迟
                videoListState.value = VideoListState.Loading
                val videos = videoScanner.scanLocalVideos(limit)
                videoListState.value = VideoListState.Success(videos)
            } catch (e: Exception) {
                videoListState.value = VideoListState.Error(e.message ?: "加载失败")
            }
        }
    }
    
    /**
     * 加载更多视频
     */
    fun loadMoreVideos(limit: Int) {
        viewModelScope.launch {
            try {
                val currentData = (videoListState.value as? VideoListState.Success)?.data ?: emptyList()
                val offset = currentData.size
                val moreVideos = videoScanner.scanLocalVideos(limit, offset)
                if (moreVideos.isNotEmpty()) {
                    val updatedList = currentData + moreVideos
                    videoListState.value = VideoListState.Success(updatedList)
                }
            } catch (e: Exception) {
                // 加载更多失败时，保持当前状态
                e.printStackTrace()
            }
        }
    }
    
    /**
     * 处理视频点击事件
     */
    fun onVideoClick(videoItem: VideoItem) {
        // 设置选中的视频并显示播放器
        selectedVideo.value = videoItem
        showPlayer.value = true
        hideBottomNavBar.value = true
        
        // 记录当前视频的索引
        if (videoListState.value is VideoListState.Success) {
            val videos = (videoListState.value as VideoListState.Success).data
            currentVideoIndex.value = videos.indexOf(videoItem)
        }
    }
    
    /**
     * 关闭播放器
     */
    fun closePlayer() {
        showPlayer.value = false
        hideBottomNavBar.value = false
        // 延迟清除选中的视频，以便动画可以正常播放
        viewModelScope.launch {
            selectedVideo.value = null
            currentVideoIndex.value = -1
        }
    }
    
    /**
     * 获取下一个视频
     */
    fun getNextVideo(): VideoItem? {
        if (videoListState.value is VideoListState.Success) {
            val videos = (videoListState.value as VideoListState.Success).data
            val nextIndex = currentVideoIndex.value + 1
            if (nextIndex < videos.size) {
                currentVideoIndex.value = nextIndex
                return videos[nextIndex]
            }
        }
        return null
    }
    
    /**
     * 获取上一个视频
     */
    fun getPreviousVideo(): VideoItem? {
        if (videoListState.value is VideoListState.Success) {
            val videos = (videoListState.value as VideoListState.Success).data
            val prevIndex = currentVideoIndex.value - 1
            if (prevIndex >= 0) {
                currentVideoIndex.value = prevIndex
                return videos[prevIndex]
            }
        }
        return null
    }
    

}